package cn.liccshop.core.service.shopGoodsAttr.service;

import cn.liccshop.core.entity.ShopAttribute;
import cn.liccshop.core.entity.ShopCategory;
import cn.liccshop.core.entity.ShopGoodsAttr;
import cn.liccshop.core.entity.ShopProducts;
import cn.liccshop.core.service.shopAttribute.mapper.ShopAttributeMapper;
import cn.liccshop.core.service.shopCategory.mapper.ShopCategoryMapper;
import cn.liccshop.core.service.shopGoodsAttr.mapper.ShopGoodsAttrMapper;
import cn.liccshop.core.service.shopGoodsAttr.service.IShopGoodsAttrService;
import cn.liccshop.core.service.shopPhoto.mapper.ShopPhotoMapper;
import cn.liccshop.core.service.shopProducts.mapper.ShopProductsMapper;
import cn.liccshop.core.utils.AttrOfProduct;
import cn.liccshop.sys.constants.Constants;
import cn.liccshop.sys.exception.MySystemException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 商品属性表 服务实现类
 * </p>
 *
 * @author LiCC
 * @since 2019-04-08
 */
@Service
public class ShopGoodsAttrServiceImpl extends ServiceImpl<ShopGoodsAttrMapper, ShopGoodsAttr> implements IShopGoodsAttrService {

    @Autowired
    private ShopCategoryMapper CategoryMapper;
    @Autowired
    private ShopAttributeMapper attributeMapper;
    @Autowired
    private ShopProductsMapper productsMapper;

    @Override
    public List<Map<String, List<ShopGoodsAttr>>> getShopGoodsAttrByGoodsId(String goodsId, String catId) throws MySystemException {
        /** 获取该商品的分类属性值 **/
        List<ShopAttribute> attributes = attributeMapper.selectList(new QueryWrapper<>(new ShopAttribute().setCatId(catId)).orderByAsc("sort_order"));
        if (CollectionUtils.isEmpty(attributes)){
            /** 属性值不为空 **/
            throw new MySystemException("该分类没有属性", Constants.SERVICE_FAIL);
        }
        if (StringUtils.isBlank(goodsId)&&StringUtils.isBlank(catId)){
            /** 商品ID或分类ID为空 **/
            throw new MySystemException((StringUtils.isBlank(goodsId)?"商品ID":"分类ID")+"参数为空", Constants.SERVICE_FAIL);
        }
        /** 属性值不为空 **/
        List<Map<String, List<ShopGoodsAttr>>>  attrInfos = new ArrayList<>();
        for (ShopAttribute attribute : attributes) {
            /** 遍历分类属性，获取属性ID **/
            /** 通过属性ID和商品ID，获取该属性的值 **/
            List<ShopGoodsAttr> goodsAttrs = baseMapper.selectList(new QueryWrapper<>(new ShopGoodsAttr().setGoodsId(goodsId).setAttrId(attribute.getAttrId())).orderByAsc("attr_sort"));
            for(ShopGoodsAttr goodsAttr : goodsAttrs){
                /** 赋值分类名称，前端需要 **/
                goodsAttr.setAttrName(attribute.getAttrName());
            }
            Map<String, List<ShopGoodsAttr>> goodsAttrsMap = new HashMap<>();
            goodsAttrsMap.put("goodsAttrs", goodsAttrs);
            attrInfos.add(goodsAttrsMap);
        }
        return attrInfos;
    }

    @Override
    public Map<String, Object> shopGoodsAttrByGoodsIdAndCatId(String goodsId, String catId) throws MySystemException {
        /** 获取该商品的分类属性值 **/
        List<ShopAttribute> attributes = attributeMapper.selectList(new QueryWrapper<>(new ShopAttribute().setCatId(catId)).orderByAsc("sort_order"));
        List<List<ShopGoodsAttr>> attributesOfAttrs = new ArrayList<>();
        Map<String, Object> data = new HashMap<>();
        String productsName = null;
        for (ShopAttribute attribute : attributes) {
            /** 遍历分类属性，获取属性ID **/
            /** 通过属性ID和商品ID，获取该属性的值 **/
            List<ShopGoodsAttr> goodsAttrs = baseMapper.selectList(new QueryWrapper<>(new ShopGoodsAttr().setGoodsId(goodsId).setAttrId(attribute.getAttrId())).orderByAsc("attr_sort"));
            for(ShopGoodsAttr goodsAttr : goodsAttrs){
                /** 赋值分类名称，前端需要 **/
                goodsAttr.setAttrName(attribute.getAttrName());
            }
            attributesOfAttrs.add(goodsAttrs);
            productsName = productsName == null ? attribute.getAttrName() : (productsName + "-" + attribute.getAttrName());
        }
        List<ShopProducts> products = AttrOfProduct.choose(attributesOfAttrs);
        data.put("productsName", productsName);
        data.put("products", products);
        data.put("productAllNumber", 0);
        return data;
    }

    @Override
    public Map<String, Object> shopProductsByGoodsIdAndCatId(String goodsId, String catId) throws MySystemException {
        /** 获取排序好的商品值 **/
        Map<String, Object> data = shopGoodsAttrByGoodsIdAndCatId(goodsId, catId);
        List<ShopProducts> products = (List<ShopProducts>) data.get("products");
        List<ShopProducts> productsDB = productsMapper.selectList(new QueryWrapper<>(new ShopProducts().setGoodsId(goodsId)));
        /** 商品库存 **/
        int productAllNumber = 0;
        for (ShopProducts product : products){
            for (ShopProducts productDB : productsDB){
                String[] goodsAttr = product.getGoodsAttr().split(":");
                String[] goodsAttrDB = productDB.getGoodsAttr().split(":");
                Arrays.sort(goodsAttr);
                Arrays.sort(goodsAttrDB);
                if (Arrays.equals(goodsAttr, goodsAttrDB)){
                    /** 属性ID相同 **/
                    product.setId(productDB.getId())
                            .setGoodsAttr(productDB.getGoodsAttr())
                            .setProductSort(productDB.getProductSort())
                            .setProductName(productDB.getProductName())
                            .setProductNumber(productDB.getProductNumber())
                            .setProductPrice(productDB.getProductPrice())
                            .setProductWarnNumber(productDB.getProductWarnNumber())
                            .setCreateBy(productDB.getCreateBy())
                            .setCreateDate(productDB.getCreateDate());
                    productAllNumber += productDB.getProductNumber();
                    break;
                }
            }
        }
        data.put("productAllNumber", productAllNumber);
        return data;
    }

}
